package com.usian.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.wemedia.WemediaContans;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMapper;
import com.usian.wemedia.service.WmNewsService;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmNewsServiceImpl extends BaseServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {
    @Value("${fdfs.url}")
    private  String baseUrl;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper  ;
    @Override
    public ResponseResult findAll(WmNewsPageReqDto wmNewsPageReqDto) {

        if (wmNewsPageReqDto==null)return errorParam;
        LambdaQueryWrapper<WmNews> wrapper=new LambdaQueryWrapper<>();
        if (wmNewsPageReqDto.getStatus()!=null)wrapper.eq(WmNews::getStatus,wmNewsPageReqDto.getStatus());
        if (wmNewsPageReqDto.getChannelId()!=null)wrapper.eq(WmNews::getChannelId,wmNewsPageReqDto.getChannelId());
        if (wmNewsPageReqDto.getKeyword()!=null)wrapper.like(WmNews::getTitle,wmNewsPageReqDto.getKeyword());
        if (wmNewsPageReqDto.getMaxTime()==null)wmNewsPageReqDto.setMaxTime(new Date());
        wrapper.lt(WmNews::getPublishTime,wmNewsPageReqDto.getMaxTime());
        wrapper.orderByDesc(WmNews::getPublishTime);
        if (ThreadLocalUtils.getUserId()==null)return needLogin;
        wrapper.eq(WmNews::getUserId,ThreadLocalUtils.getUserId());
        return gePageDate(wmNewsPageReqDto,wrapper);
    }

    @Override
    public ResponseResult saveNews(WmNewsDto wmNews, Short isSubmit) {
        if (wmNews == null) return errorParam;


        WmNews news = new WmNews();
        BeanUtils.copyProperties(wmNews, news);
        if (wmNews.getId() == null) {
            save(news);
            Integer newsId= news.getId();
            String content = news.getContent();
            JSONArray array= JSON.parseArray(content);
            List<String> materialUrlList_content=new ArrayList<>();
            array.forEach(o -> {
                JSONObject object= JSON.parseObject(o.toString());
                if (object.getString("type").equals("image")){
                    String value = object.getString("value");
                    materialUrlList_content.add(value.replace(baseUrl,""));
                }

            });
            LambdaQueryWrapper<WmMaterial> materialWrapper = new LambdaQueryWrapper<>();
            materialWrapper.eq(WmMaterial::getUserId,ThreadLocalUtils.getUserId());
            materialWrapper.in(WmMaterial::getUrl,materialUrlList_content);
            List<WmMaterial> list = wmMaterialMapper.selectList(materialWrapper);
            List<String> ids=list.stream().map(m->m.getId()+"").collect(Collectors.toList());Collectors.toList();
           wmMaterialMapper.saveRelationsByContent(ids,newsId,WemediaContans.CANCEL_COLLECT_MATERIAL);
        } else {
            updateById(news);
        }
        return null;


    }
}
